<?php
namespace Home\Controller;
use Think\Controller;
class SetupController extends Controller {

    public function index()
    {
        $rztype = M('gy_rz_ger')->where(['uid'=>$_SESSION['id']])->getField('rztype');
        $this->assign('rztype',$rztype);

        $this->display();
    }

    //个人认证
    public function ger()
    {
        if (IS_POST) {
            $data['sfzname'] = I('post.sfzname');
            $data['sfznum'] = I('post.sfznum');
            $data['gsname'] = I('post.gsname');
            $data['jianjie'] = I('post.jianjie');
            $data['phone'] = I('post.phone');

            $data['rztype'] = 1;//个人
            $send_to = I('post.yzm');

            //身份证
            $sfz_yz = preg_match("/^(\d{18,18}|\d{15,15}|\d{17,17}x)$/",$data['sfznum']);
            if (!$sfz_yz) {
                $arr = array(
                    'error' =>0,
                    'info' => '请输入正确的身份证号'  
                    );
                echo json_encode($arr);
                exit;
            }
            //验证手机
             $preg_js = "/^1[3458][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $data['phone']);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }
            //短信随机码验证
            if ($send_to != $_SESSION['sj_mobile']) {
                $arr = array(
                    'error' => 0,
                    'info' => '验证码错误'
                    );
                echo json_encode($arr);
                exit;
            }

            //判断是否有认证信息
            $res_info = M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->find();
            if ($res_info) {
                //存在更新
                M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->save($data);
            }else {
                //不存在添加
                $data['uid'] = $_SESSION['id'];
                $data['time'] = time();
                M('gy_rz_ger')->add($data);
            }
            //$url = U('setup/chuangj',['rztype'=>$data['rztype']]);
            $arr = array(
                'error' => 1,
                'info' => '提交成功',
                'url' => $url
                );
            echo json_encode($arr);

        }else {
            $res_info = M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->find();
            if ($res_info) {
                //检测乱捣乱的人
                if ($res_info['rztype'] == 1) {
                    $this->assign('res_info',$res_info);
                    $this->display(); 
                }else {
                    $this->error('别捣乱');
                }
            }else {
                $this->display(); 
            }
        }
        
    }
    //身份证正面
    public function sfzz()
    {
        $upload = new \Think\Upload();// 实例化上传类    
        $upload->maxSize   =     3145728 ;// 设置附件上传大小    
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
        $upload->savePath  =      'Uploads/sfzimg/'; // 设置附件上传目录    
        // 上传文件     
        $info   =   $upload->upload();    
        if(!$info) {// 上传错误提示错误信息         
            $arr = array(
            'code' => 0,
            'msg' => $upload->getError(),
            );
            echo json_encode($arr);
        }else{// 上传成功   
            $info = current($info);//减维 
            //绝对路径
            $url =  __ROOT__.'/'.'Uploads/'.$info['savepath'].$info['savename'];   
            //相对路径
            $path = '/'.'Uploads/'.$info['savepath'].$info['savename'];

            //判断是否有认证信息
            $res_info = M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->find();
            if ($res_info) {
                //删除旧图片
                $oldpath = M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->getField('sfzz');
                unlink('./'.$oldpath);
                //入库
                M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->setField('sfzz',$path);
            }else {
                //新添加
                $data['uid'] = $_SESSION['id'];
                $data['sfzz'] = $path;
                $data['time'] = time();
                M('gy_rz_ger')->add($data);
            }

            $arr = array(
            'code' => 0,
            'msg' => '上传成功',
            'data'=> array(
                'src' => $url,
                'title' => $info['savename']
                )
            );
            echo json_encode($arr);
        }
    }
    //身份证反面
    public function sfzf()
    {
        $upload = new \Think\Upload();// 实例化上传类    
        $upload->maxSize   =     3145728 ;// 设置附件上传大小    
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
        $upload->savePath  =      'Uploads/sfzimg/'; // 设置附件上传目录    
        // 上传文件     
        $info   =   $upload->upload();    
        if(!$info) {// 上传错误提示错误信息         
            $arr = array(
            'code' => 0,
            'msg' => $upload->getError(),
            );
            echo json_encode($arr);
        }else{// 上传成功   
            $info = current($info);//减维 
            //绝对路径
            $url =  __ROOT__.'/'.'Uploads/'.$info['savepath'].$info['savename'];   
            //相对路径
            $path = '/'.'Uploads/'.$info['savepath'].$info['savename'];

            //判断是否有认证信息
            $res_info = M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->find();
            if ($res_info) {
                //删除旧图片
                $oldpath = M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->getField('sfzf');
                unlink('./'.$oldpath);
                //入库
                M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->setField('sfzf',$path);
            }else {
                //新添加
                $data['uid'] = $_SESSION['id'];
                $data['sfzf'] = $path;
                $data['time'] = time();
                M('gy_rz_ger')->add($data);
            }

            $arr = array(
            'code' => 0,
            'msg' => '上传成功',
            'data'=> array(
                'src' => $url,
                'title' => $info['savename']
                )
            );
            echo json_encode($arr);
        }
    }

    //学校认证
    public function sch()
    {
        if (IS_POST) {
            $data['sfzname'] = I('post.sfzname');
            $data['sfznum'] = I('post.sfznum');
            $data['gsname'] = I('post.gsname');
            $data['jianjie'] = I('post.jianjie');
            $data['phone'] = I('post.phone');

            $data['rztype'] = 2;//sch
            $send_to = I('post.yzm');

            //身份证
            $sfz_yz = preg_match("/^(\d{18,18}|\d{15,15}|\d{17,17}x)$/",$data['sfznum']);
            if (!$sfz_yz) {
                $arr = array(
                    'error' =>0,
                    'info' => '请输入正确的身份证号'  
                    );
                echo json_encode($arr);
                exit;
            }
            //验证手机
             $preg_js = "/^1[3458][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $data['phone']);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }
            //短信随机码验证
            if ($send_to != $_SESSION['sj_mobile']) {
                $arr = array(
                    'error' => 0,
                    'info' => '验证码错误'
                    );
                echo json_encode($arr);
                exit;
            }

            //判断是否有认证信息
            $res_info = M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->find();
            if ($res_info) {
                //存在更新
                M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->save($data);
            }else {
                //不存在添加
                $data['uid'] = $_SESSION['id'];
                $data['time'] = time();
                M('gy_rz_ger')->add($data);
            }
            //$url = U('setup/chuangj',['rztype'=>$data['rztype']]);
            $arr = array(
                'error' => 1,
                'info' => '提交成功',
                'url' => $url
                );
            echo json_encode($arr);
        }else {
            $res_info = M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->find();
            if ($res_info) {
                //检测乱捣乱的人
                if ($res_info['rztype'] == 2) {
                    $this->assign('res_info',$res_info);
                    $this->display(); 
                }else {
                    $this->error('别捣乱');
                }
            }else {
                $this->display(); 
            }
        }
    }

    //企业认证
    public function qiy()
    {
        if (IS_POST) {
            $data['sfzname'] = I('post.sfzname');
            $data['sfznum'] = I('post.sfznum');
            $data['gsname'] = I('post.gsname');
            $data['jianjie'] = I('post.jianjie');
            $data['phone'] = I('post.phone');

            $data['rztype'] = 3;//qiy
            $send_to = I('post.yzm');

            //验证手机
             $preg_js = "/^1[3458][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $data['phone']);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }
            //短信随机码验证
            if ($send_to != $_SESSION['sj_mobile']) {
                $arr = array(
                    'error' => 0,
                    'info' => '验证码错误'
                    );
                echo json_encode($arr);
                exit;
            }

            //判断是否有认证信息
            $res_info = M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->find();
            if ($res_info) {
                //存在更新
                M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->save($data);
            }else {
                //不存在添加
                $data['uid'] = $_SESSION['id'];
                $data['time'] = time();
                M('gy_rz_ger')->add($data);
            }
            //$url = U('setup/chuangj',['rztype'=>$data['rztype']]);
            $arr = array(
                'error' => 1,
                'info' => '提交成功',
                'url' => $url
                );
            echo json_encode($arr);

        }else {
            $res_info = M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->find();
            if ($res_info) {
                //检测乱捣乱的人
                if ($res_info['rztype'] == 3) {
                    $this->assign('res_info',$res_info);
                    $this->display(); 
                }else {
                    $this->error('别捣乱');
                }
            }else {
                $this->display(); 
            }
        }
    }

    //慈善认证
    public function cis()
    {
        if (IS_POST) {
            $data['sfzname'] = I('post.sfzname');
            $data['sfznum'] = I('post.sfznum');
            $data['gsname'] = I('post.gsname');
            $data['jianjie'] = I('post.jianjie');
            $data['phone'] = I('post.phone');

            $data['rztype'] = 4;//qiy
            $send_to = I('post.yzm');

            //验证手机
             $preg_js = "/^1[3458][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $data['phone']);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }
            //短信随机码验证
            if ($send_to != $_SESSION['sj_mobile']) {
                $arr = array(
                    'error' => 0,
                    'info' => '验证码错误'
                    );
                echo json_encode($arr);
                exit;
            }

            //判断是否有认证信息
            $res_info = M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->find();
            if ($res_info) {
                //存在更新
                M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->save($data);
            }else {
                //不存在添加
                $data['uid'] = $_SESSION['id'];
                $data['time'] = time();
                M('gy_rz_ger')->add($data);
            }
            //$url = U('setup/chuangj',['rztype'=>$data['rztype']]);
            $arr = array(
                'error' => 1,
                'info' => '提交成功',
                'url' => $url
                );
            echo json_encode($arr);

        }else {
            $res_info = M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->find();
            if ($res_info) {
                //检测乱捣乱的人
                if ($res_info['rztype'] == 4) {
                    $this->assign('res_info',$res_info);
                    $this->display(); 
                }else {
                    $this->error('别捣乱');
                }
            }else {
                $this->display(); 
            }
        }
    }

    //创建项目
    public function chuangj()
    {
        $rztype = I('get.rztype');
        $this->assign('rztype',$rztype);

        if (IS_POST) {
            $data['xm_name'] = I('post.xm_name');
            $data['xm_content'] = I('post.xm_content');
            $data['xm_type'] = I('post.xm_type');
            $data['xm_diyu'] = I('post.xm_diyu');
            $data['xm_xiangq'] = I('post.xm_xiangq');
            $data['xm_days'] = I('post.xm_days');
            $data['xm_mubiao'] = I('post.xm_mubiao');
            $data['xm_list'] = I('post.xm_list');

            $data['xm_img'] = I('post.xm_img');
            $data['uid'] = $_SESSION['id'];

            $data['rztype'] = I('post.rztype');

            $data['time'] = time();
            //检测是否添加图片
            if (empty($data['xm_img'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '请添加项目背景图'
                );
                echo json_encode($arr);
                exit;
            }
            //检测是否为整数
            if (!ctype_digit($data['xm_days'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '检查天数是否为整数'
                );
                echo json_encode($arr);
                exit;
            }
            //检测是否为整数
            if (!ctype_digit($data['xm_mubiao'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '检查目标是否为整数'
                );
                echo json_encode($arr);
                exit;
            }
            //添加项目
            $res = M('gy_xm')->add($data);
            if ($res) {
                $url = U('setup/ok');
                $arr = array(
                    'error' => 1,
                    'info' => '项目创建成功',
                    'url' => $url
                );
                echo json_encode($arr);

                //清空session.xmimg
                $_SESSION['xmimg'] = '';
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '项目创建失败'
                );
                echo json_encode($arr);
            }

        }else {
            $this->display();
        }    
    }

    //项目图片
    public function xmimg()
    {
        $upload = new \Think\Upload();// 实例化上传类    
        $upload->maxSize   =     3145728 ;// 设置附件上传大小    
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
        $upload->savePath  =      'Uploads/xmimg/'; // 设置附件上传目录    
        // 上传文件     
        $info   =   $upload->upload();    
        if(!$info) {// 上传错误提示错误信息         
            $arr = array(
            'code' => 0,
            'msg' => $upload->getError(),
            );
            echo json_encode($arr);
        }else{// 上传成功   
            $info = current($info);//减维 
            //绝对路径
            $url =  __ROOT__.'/'.'Uploads/'.$info['savepath'].$info['savename'];   
            //相对路径
            $path = '/'.'Uploads/'.$info['savepath'].$info['savename'];

            if (I('get.id') == '') {
                //判断是否有上传图
                if ($_SESSION['xmimg']) {
                    //删除旧图片
                    $oldpath = $_SESSION['xmimg'];
                    unlink('./'.$oldpath);
                    $_SESSION['xmimg'] = $path;
                }else {
                    //新添加
                    $_SESSION['xmimg'] = $path;
                }
                $arr = array(
                'code' => 0,
                'msg' => '上传成功',
                'data'=> array(
                    'src' => $url,
                    'path' => $path
                    )
                );
                echo json_encode($arr);
            }else {
                //删除旧图片
                $oldpath = M('gy_xm')->where(['id'=>I('get.id')])->getField('xm_img');
                unlink('./'.$oldpath);
                //入库
                M('gy_xm')->where(['id'=>I('get.id')])->setField('xm_img',$path);
                $arr = array(
                'code' => 0,
                'msg' => '上传成功',
                'data'=> array(
                    'src' => $url,
                    'path' => $path
                    )
                );
                echo json_encode($arr);
            }
        }
    }

    //项目编辑
    public function editxm()
    {   
        if (IS_POST) {
            $id = I('post.id');
            $data['xm_name'] = I('post.xm_name');
            $data['xm_content'] = I('post.xm_content');
            $data['xm_type'] = I('post.xm_type');
            $data['xm_diyu'] = I('post.xm_diyu');
            $data['xm_xiangq'] = I('post.xm_xiangq');
            $data['xm_days'] = I('post.xm_days');
            $data['xm_mubiao'] = I('post.xm_mubiao');
            $data['xm_list'] = I('post.xm_list');

            //检测是否为整数
            if (!ctype_digit($data['xm_days'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '检查天数是否为整数'
                );
                echo json_encode($arr);
                exit;
            }
            //检测是否为整数
            if (!ctype_digit($data['xm_mubiao'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '检查目标是否为整数'
                );
                echo json_encode($arr);
                exit;
            }
            //添加项目
            $res = M('gy_xm')->where(['id'=>$id])->save($data);
            if ($res) {
                $url = U('user/myxm');
                $arr = array(
                    'error' => 1,
                    'info' => '项目编辑成功',
                    'url' => $url
                );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '项目编辑失败'
                );
                echo json_encode($arr);
            }
            
        }else {
            $id = I('get.id');
            $infoxm = M('gy_xm')->where(['id'=>$id])->find();
            $this->assign('infoxm',$infoxm); 

            $this->display();
        }
    }

    //成功提示
    public function ok()
    {
        $this->display();
    }



}